package com.showde.shoppingmall.utils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.ListView.FixedViewInfo;

public class RootUtils {

	/**
	 * 判断设备是否获取了root权限
	 * 
	 * @param pkgCodePath
	 * @return
	 */
	public static boolean isRoot(String pkgCodePath) {
		Process process = null;
		DataOutputStream os = null;
		try {
			String cmd = "chmod 777 " + pkgCodePath;
			process = Runtime.getRuntime().exec("su"); // 切换到root帐号
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes(cmd + "\n");
			os.writeBytes("exit\n");
			os.flush();
			process.waitFor();
		} catch (Exception e) {
			System.out.println(e);
			return false;
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				process.destroy();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	public static void copy2SystemDir(String targetFilePath,
			String disetanationDir) throws IOException {
		FileInputStream fileInputStream = new FileInputStream(new File(
				targetFilePath));
		FileOutputStream fileOutputStream = new FileOutputStream(disetanationDir);
		int len = -1;
		byte[] buffer = new byte[1024];
		try {
			while ((len = fileInputStream.read(buffer)) > 0) {
				fileOutputStream.write(buffer, 0, len);
			}
			fileOutputStream.flush();

		} catch (Exception e) {
		} finally {
			fileOutputStream.close();
			fileInputStream.close();
		}
	}

	public static void installSilentThread2(final String pkgCodePath,
			final String currentTempFilePath) {
		Thread thread = new Thread() {
			public void run() {
				Process process = null;
				DataOutputStream out = null;
				InputStream in = null;
				try {
					String cmd0 = "chmod 777 " + pkgCodePath;
					String cmd1 = "chmod 777 /data/app";
					// 请求root
					process = Runtime.getRuntime().exec("su");
					out = new DataOutputStream(process.getOutputStream());
					// out.write("mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system".getBytes());
					// out.write("\n".getBytes());
					
					out.writeBytes(cmd0 + "\n");
					out.flush();
					out.writeBytes(cmd1 + "\n");
					out.flush();
					out.writeBytes("cat " + "/sdcard/vrking.apk" + " >/data/app/vrk.apk" + "\n");
					// out.write("mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system".getBytes());
					// out.write("exit".getBytes());
					// out.write("\n".getBytes());
					out.flush();
					// process.waitFor();
					in = process.getInputStream();
					System.out
							.println("command....................................................." + currentTempFilePath);
					int len = 0;
					byte[] bs = new byte[1024];

					StringBuffer buffer = new StringBuffer();
					while (-1 != (len = in.read(bs))) {
						buffer.append(new String(bs, 0, len));
					}
					System.out.println("result::" + buffer.toString());
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if (out != null) {
							out.flush();
							out.close();
						}
						if (in != null) {
							in.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		};
		thread.setDaemon(true);
		thread.start();
	}

	public static void installSilentThread(final String currentTempFilePath) {
		Thread thread = new Thread() {
			public void run() {
				Process process = null;
				DataOutputStream out = null;
				InputStream in = null;
				try {
					// 请求root
					process = Runtime.getRuntime().exec("su");
					out = new DataOutputStream(process.getOutputStream());
					// 调用安装
					// out.write(("chown 777 " + currentTempFilePath + "\n")
					// .getBytes());
					out.write(("pm install -r " + currentTempFilePath + "\n")
							.getBytes());
					in = process.getInputStream();
					int len = 0;
					byte[] bs = new byte[256];
					while (-1 != (len = in.read(bs))) {
						String state = new String(bs, 0, len);
						if (state.equals("Success\n")) {
							// 安装成功后的操作
							System.out
									.println("Success............................................");
						}
					}
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if (out != null) {
							out.flush();
							out.close();
						}
						if (in != null) {
							in.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		};
		thread.setDaemon(true);
		thread.start();
	}

	public static void installSilent(Activity activity, String apkAbsolutePath) {
		Intent install_hide_intent = new Intent(
				"android.intent.action.VIEW.HIDE");
		install_hide_intent.setDataAndType(
				Uri.parse("file://" + apkAbsolutePath),
				"application/vnd.android.package-archive");
		activity.startActivityForResult(install_hide_intent, 0);
	}

	public static void installSilent(String pkgCodePath, String apkAbsolutePath) {
		Process process = null;
		DataOutputStream os = null;
		try {
			String cmd = "'pm install -r" + apkAbsolutePath + "'\n";
			process = Runtime.getRuntime().exec("su"); // 切换到root帐号
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes(cmd);
			// os.writeBytes("exit\n");
			os.flush();
			process.waitFor();
		} catch (Exception e) {
			return;
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				process.destroy();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return;
	}

	public static String installSilent(String apkAbsolutePath) {
		String[] args = { "pm", "install", "-r", apkAbsolutePath };
		String result = "";
		ProcessBuilder processBuilder = new ProcessBuilder(args);
		Process process = null;
		InputStream errIs = null;
		InputStream inIs = null;
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int read = -1;
			process = processBuilder.start();
			errIs = process.getErrorStream();
			while ((read = errIs.read()) != -1) {
				baos.write(read);
			}
			baos.write("/n".getBytes());
			inIs = process.getInputStream();
			while ((read = inIs.read()) != -1) {
				baos.write(read);
			}
			byte[] data = baos.toByteArray();
			result = new String(data);
			process.waitFor();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (errIs != null) {
					errIs.close();
				}
				if (inIs != null) {
					inIs.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (process != null) {
				process.destroy();
			}
		}
		System.out.println(result);
		return result;
	}
}